num genre
1 125 Horror
2 110 Thrillers
3 38 Comedies
4 19 SciFi&Fantasy
5 12 Cult
6 2 Documentaties
7 2 Romantic
December 9, 2024
CI <- function(data, coverage_prob){
#Generates a normal prediction interval with an intended coverage probability of coverage_prob based on a vector of numeric data
lower_zscore <- qnorm((1-coverage_prob)/2)
upper_zscore <- qnorm(((1-coverage_prob)/2) + coverage_prob)
avg <- mean(data)
stan_d <- sd(data)
lower_bound <- avg + lower_zscore*stan_d
upper_bound <- avg + upper_zscore*stan_d
return(data.frame(PI_percentage = coverage_prob, lower = lower_bound, upper = upper_bound))
}one_beta_simulation <- function(n, alpha, beta, ci_prop){
#Assesses prediction accuracy and actual coverage probability of a normal prediction interval when used on a vector of numeric data of size n. The numeric data is generated from a beta distribution with parameters alpha and beta.
cover_df <- CI(rbeta(n, alpha, beta), ci_prop)
cover_prop <- pbeta(cover_df[1, "upper"], alpha, beta) - pbeta(cover_df[1, "lower"], alpha, beta)
mean_in_interval <- .5 >= cover_df[1, "lower"] & .5 <= cover_df[1,"upper"]
param_df <- data.frame(cover = cover_prop, alpha = rep(alpha, nrow(cover_df)), beta = rep(beta, nrow(cover_df)), mean_in_interval = mean_in_interval)
df <- cbind(cover_df, param_df)
return(df)
}beta_sims_n <- function(n){
#Iterates over a vector of possible alpha = beta values and applies one_beta_simulation to each possible value of alpha/beta. All simulations use data of sample size n.
df1 <- map(parameters,\(param) one_beta_simulation(n, param, param, ci) ) %>%
list_rbind()
df2 <- data.frame(n = rep(n, nrow(df1)))
df <- cbind(df2, df1)
return(df)
} n PI_percentage lower upper cover alpha beta mean_in_interval
1 455 0.95 0.4518613 0.5493738 0.9449629 193 193 TRUE
2 370 0.95 0.4073973 0.5872232 0.9461831 57 57 TRUE
3 32 0.95 0.2988644 0.6482433 0.9203282 13 13 TRUE
4 452 0.95 0.4412301 0.5585745 0.9327327 121 121 TRUE
5 474 0.95 0.4474210 0.5559983 0.9541341 169 169 TRUE
6 381 0.95 0.4275185 0.5746540 0.9428899 83 83 TRUE
7 265 0.95 0.4457077 0.5515220 0.9484484 169 169 TRUE
8 92 0.95 0.4444017 0.5499518 0.9579876 187 187 TRUE
9 55 0.95 0.4454341 0.5509372 0.9636713 197 197 TRUE
10 56 0.95 0.4414381 0.5691853 0.9253503 99 99 TRUE